package com.huilian.iotbox.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.data.dao.CommoditySupplierCommonDao;
import com.huilian.iotbox.data.dto.CommodityStockItemDto;
import com.huilian.iotbox.data.dto.CommoditySupplierContactDto;
import com.huilian.iotbox.data.dto.CommoditySupplierDto;
import com.huilian.iotbox.data.service.CommodityStockItemCommonService;
import com.huilian.iotbox.data.service.CommoditySupplierCommonService;
import com.huilian.iotbox.data.service.CommoditySupplierContactCommonService;
import com.huilian.iotbox.data.vo.CommodityStockItemVo;
import com.huilian.iotbox.data.vo.CommoditySupplierContactVo;
import com.huilian.iotbox.data.vo.CommoditySupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CommoditySupplierCommonServiceImpl implements CommoditySupplierCommonService {
    @Autowired
    private CommoditySupplierCommonDao commoditySupplierDao;
    @Resource
    private CommoditySupplierContactCommonService commoditySupplierContactService;
    @Resource
    private CommodityStockItemCommonService commodityStockItemService;

    @Override
    public List<CommoditySupplierVo> initSuppliers(CommoditySupplierDto commoditySupplierDto) {
        if (commoditySupplierDto.getUserInfo().getRoleId().equals(5)) {
            commoditySupplierDto.setUserId(commoditySupplierDto.getUserInfo().getParentId());
        } else {
            commoditySupplierDto.setUserId(commoditySupplierDto.getUserInfo().getId());
        }
        return commoditySupplierDao.findSupplierByUserId(commoditySupplierDto);
    }

    @Override
    public PageInfo<CommoditySupplierVo> initSuppliersPageInfo(CommoditySupplierDto commoditySupplierDto) {
        commoditySupplierDto.setUserId(commoditySupplierDto.getUserInfo().getMchStoreUserId());
        PageHelper.orderBy("cs.createTime desc");
        PageHelper.startPage(commoditySupplierDto.getPageNum(), commoditySupplierDto.getPageSize(), true);
        List<CommoditySupplierVo> list = commoditySupplierDao.findSupplierByUserId(commoditySupplierDto);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSuppliers(CommoditySupplierDto commoditySupplierDto) throws Exception {
        commoditySupplierDto.setUserId(commoditySupplierDto.getUserInfo().getMchStoreUserId());
        commoditySupplierDto.setPurchaseQuantity(0);
        commoditySupplierDto.setPurchaseAmount(BigDecimal.ZERO);
        if (ObjectUtils.isEmpty(commoditySupplierDto.getFullName())) {
            throw new Exception("缺少供应商名字");
        }
        if (ObjectUtils.isEmpty(commoditySupplierDto.getForShort())) {
            throw new Exception("缺少供应商简称");
        }
        if (ObjectUtils.isEmpty(commoditySupplierDto.getAddress())) {
            throw new Exception("缺少供应商地址");
        }
        int result = 0;
        CommoditySupplierVo commoditySupplierVo = commoditySupplierDao.findSupplierGetVersion(commoditySupplierDto);
        if (commoditySupplierDto.getMethodType().equals(0)) {
            commoditySupplierDto.setVersion(0);
            result = commoditySupplierDao.add(commoditySupplierDto);
        } else if (commoditySupplierDto.getMethodType().equals(1)) {
            commoditySupplierDto.setVersion(commoditySupplierVo.getVersion());
            commoditySupplierDto.setPurchaseQuantity(commoditySupplierVo.getPurchaseQuantity());
            commoditySupplierDto.setPurchaseAmount(commoditySupplierVo.getPurchaseAmount());
            result = commoditySupplierDao.update(commoditySupplierDto);
        }
        if (result < 1) {
            throw new Exception("操作失败，请查看输入信息是否正常");
        }
        if (commoditySupplierDto.getMethodType().equals(1)) {
            List<CommoditySupplierContactVo> commoditySupplierContactVoList = commoditySupplierContactService.findBySupplierId(commoditySupplierVo.getId());
            for (CommoditySupplierContactVo commoditySupplierContactVo : commoditySupplierContactVoList) {
                commoditySupplierContactService.delSuppliersContact(commoditySupplierContactVo.getId());
            }
        }
        for (CommoditySupplierContactVo commoditySupplierContactVo : commoditySupplierDto.getCommoditySupplierContactVoList()) {
            CommoditySupplierContactDto commoditySupplierContactDto = new CommoditySupplierContactDto();
            commoditySupplierContactDto.setSupplierId(commoditySupplierDto.getId());
            commoditySupplierContactDto.setName(commoditySupplierContactVo.getName());
            commoditySupplierContactDto.setPhone(commoditySupplierContactVo.getPhone());
            int result1 = commoditySupplierContactService.addSuppliersContact(commoditySupplierContactDto);
            if (result1 < 1) {
                throw new Exception("操作失败，请查看输入信息是否正常");
            }
        }
    }

    @Override
    public List<CommoditySupplierVo> getSupplierByUserId(Integer userId) {
        CommoditySupplierDto commoditySupplierDto = new CommoditySupplierDto();
        commoditySupplierDto.setUserId(userId);
        return commoditySupplierDao.findSupplierNameByUserId(commoditySupplierDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delSupplierById(CommoditySupplierDto commoditySupplierDto) throws Exception {
        CommoditySupplierVo commoditySupplierVo = commoditySupplierDao.findSupplierGetVersion(commoditySupplierDto);
        CommodityStockItemDto commodityStockItemDto = new CommodityStockItemDto();
        commodityStockItemDto.setCommoditySupplierId(commoditySupplierVo.getId());
        List<CommodityStockItemVo> commodityStockItemVoList = commodityStockItemService.findCommodityBySupplierId(commodityStockItemDto);
        if (commodityStockItemVoList.size() > 0) {
            throw new Exception("该供应商已存在库存商品，暂不予删除");
        }
        commoditySupplierDto.setId(commoditySupplierVo.getId());
        commoditySupplierDto.setVersion(commoditySupplierVo.getVersion());
        int result = commoditySupplierDao.updateIsDelete(commoditySupplierDto);
        if (result < 1) {
            throw new Exception("供应商删除出错");
        }
    }

    @Override
    public CommoditySupplierVo getSupplierById(Integer id) throws Exception {
        CommoditySupplierDto commoditySupplierDto = new CommoditySupplierDto();
        commoditySupplierDto.setId(id);
        return commoditySupplierDao.findSupplierGetVersion(commoditySupplierDto);
    }

    @Override
    public int updatePurchase(CommoditySupplierDto commoditySupplierDto) throws Exception {
        return commoditySupplierDao.updatePurchase(commoditySupplierDto);
    }

}
